package com.yzjs.localhost.service;

import com.yzjs.common.util.IDUtils;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.common.util.httpclient.HttpUtils;
import com.yzjs.localhost.dictionaries.DealDictionary;
import com.yzjs.localhost.dictionaries.ResourceDictionary;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.dto.VerifyDTO;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.entity.tNeusoftBigDataTesting.TNeusoftBigDataTestingEntity;
import com.yzjs.localhost.service.tNeusoftBigDataTesting.TNeusoftBigDataTestingService;
import com.yzjs.localhost.service.tNeusoftComplianceTesting.impl.TNeusoftComplianceTestingServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 大数据核验能力信息
 */
@Service("truthCheckingAbilityService")
public class TruthCheckingAbilityService {

    private static Logger logger = LogManager.getLogger(TNeusoftComplianceTestingServiceImpl.class.getName());

    @Value("${bdms.url}")
    private String  bdmsUrl ;


    @Autowired
    private TNeusoftBigDataTestingService  neusoftBigDataTestingService ;

    /**
     * 核验信息结果
     */
    public String VerificationAbilityIdNumber(String idNum,String xm) throws Exception {
        if(!IDUtils.isIDNumber(idNum)){
            return "身份证号无效";
        }
        VerifyDTO  verifyParam  = new VerifyDTO();
        verifyParam.setDatasource_id(3);
        verifyParam.setKey(idNum);
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("XM",xm);
        verifyParam.setDataList(paramMap);
        //远程调用服务能力接口
        String url = bdmsUrl+"/v1/basis_highlevel/verify";
        String json = JsonUtil.obj2json(verifyParam);
        logger.info(url);
        logger.info(json);
        // 调用合约调用
        String resultOut  = HttpUtils.httpPostJson(url,json,15000);
        logger.info("---------------------VerificationAbilityIdNumber  返回结果信息 "+resultOut);
        if(resultOut.contains("Verify successfully")){
            return "";
        }else if (resultOut.contains("Verify, field")){
            return "身份证号与姓名不匹配";
        }
        return "在襄阳公安人口库未能查到";
    }


    /**
     *  获取资源信息
     */
    public void  VerificationAbilityIdNumber(DataBundlingDTO dto, UserRightsDTO rights ) throws Exception {
        /*
         * 判断资源类型，如果 个人简历信息，家庭成员信息，人员招聘信息
         * 获取身份证号，以及姓名信息
         */
        String idCard = "";
        String xm="";
        String credentialsType ="";
        TDataPersistenceBeforceEntity beforce = dto.getBeforce();
        Map<String,Object> afterMap =JsonUtil.json2map(dto.getAfter().getInfo())  ;
        switch (beforce.getDataResources()){
            case  ResourceDictionary.HRM_FAMILY_MEMBERS:
                credentialsType =  (String) afterMap.get("famCredentials");
                idCard = (String) afterMap.get("famNums");
                xm = (String) afterMap.get("famName");
                break;
            case  ResourceDictionary.HRM_RECRUITMENT:
                credentialsType =  (String) afterMap.get("credentialsType");
                idCard = (String) afterMap.get("credentialsNum");
                xm = (String) afterMap.get("fullName");
                break;
            case  ResourceDictionary.HRM_USER:
                credentialsType =  (String) afterMap.get("credentialsType");
                idCard = (String) afterMap.get("credentialsNum");
                xm = (String) afterMap.get("fullName");
                break;
        }
        if("1".equals(credentialsType)){
           String result =  VerificationAbilityIdNumber(idCard,xm);
           if (result.length() >0){
              // 记录错误信息记录
               TNeusoftBigDataTestingEntity bigDataTesting  =new TNeusoftBigDataTestingEntity();
               bigDataTesting.setJurisdictionCode(beforce.getOperatorCode());
               bigDataTesting.setJurisdiction(AnalysisService.menusMap.get(beforce.getOperatorCode()));
               bigDataTesting.setAbstractName(result);
               bigDataTesting.setDataIdentification(beforce.getDataIdentification());
               bigDataTesting.setDisposalStatus(DealDictionary.UNTREATED);
               bigDataTesting.setWarningTime(new Date());
               bigDataTesting.setPushTime(Date.from( beforce.getCreateDate().atZone( ZoneId.systemDefault()).toInstant()) ) ;
               bigDataTesting.setResourceType(beforce.getDataResources());
               bigDataTesting.setResourceName(ResourceDictionary.conversionMap.get(beforce.getDataResources()));
               String warningInfo ="经过与大数据中心【襄阳市公安人口库】比对发现,该东软医疗资源数据资源【"+
                       ResourceDictionary.conversionMap.get(beforce.getDataResources())+ "】 检测"+result+"。 特此报警";
               bigDataTesting.setWarningInfo(warningInfo);
               bigDataTesting.setDataSourceName("襄阳市公安人口库");
               bigDataTesting.setDocumentTypeName("身份证");
               bigDataTesting.setIdentificationNumber(idCard);
               bigDataTesting.setIdName(xm);

               if(rights != null){
                   bigDataTesting.setOperationId(rights.getUserId().intValue());
                   bigDataTesting.setOperationName(rights.getNickName());
                   bigDataTesting.setOperationRoleId(rights.getRoleId().intValue());
                   bigDataTesting.setOperationRoleName(rights.getRoleName());
                   bigDataTesting.setOperationDepartId(rights.getDeptId().intValue());
                   bigDataTesting.setOperationDepartName(rights.getDeptName());
               }
               neusoftBigDataTestingService.insert(bigDataTesting);

           }
        }

    }


}
